package com.ayf.payment.game.api.channel.jiabei.util;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;


public class StringHelper {

	/*MD5ǩ��*/
	public static String EncodeMD5(String s) {
	    try {
	        MessageDigest md = MessageDigest.getInstance("MD5");
	        byte[] bytes = md.digest(s.getBytes("utf-8"));
	        return toHex(bytes);
	    }
	    catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}

	private static String toHex(byte[] bytes) {

	    final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
	    StringBuilder ret = new StringBuilder(bytes.length * 2);
	    for (int i=0; i<bytes.length; i++) {
	        ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
	        ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
	    }
	    return ret.toString();
	}

	
	/*ASCII����*/
	public static String createSign(SortedMap<String,String> parameters){
	    StringBuffer sbkey = new StringBuffer();
	    Set es = parameters.entrySet();  //���в��봫�εĲ�������accsii��������
	    Iterator it = es.iterator();
	    while(it.hasNext()) {
	        Map.Entry entry = (Map.Entry)it.next();
	        String k = (String)entry.getKey();
	        Object v = entry.getValue();
	        //��ֵ�����ݣ�������ǩ���鴮
	        if(null != v && !"".equals(v)) {
	            sbkey.append(k + "=" + v + "&");
	        }
	    }
	    return sbkey.toString();
	}

	
	
	
	
	
}
